#include "Axe.h"

#include "Resource.h"

Axe::Axe(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, bool isLeft) : Object(x, y)
{	 
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(ResourceMng::GetInst(d3ddv, BackBuffer)->getMapTexture()[14]->getFileName()), 125, 0);
	 
	this->pos_x = x;
	this->pos_y = y;
	this->veloc_x = AXE_VX;
	this->veloc_y = AXE_VY;
	this->accel_y = AXE_GRAVITY;

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	if (isLeft == true)
	{
		/*p_sprite->setFrame(0,3);
		p_sprite->setIndex(0);*/
		this->isLeft = true;
		this->veloc_x = -AXE_VX;
		p_sprite->setFrame(0,4);	//frame an
	} 
	else
	{
		/*p_sprite->setFrame(3,6);
		p_sprite->setIndex(3);*/
		this->isLeft = false;
		p_sprite->setFrame(4,8);	//frame an
		p_sprite->setIndex(4);
	}
	//p_sprite->setIndexFrame(0);

	obj_state = IS_ACTIVING;

}

Axe::~Axe()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void Axe::render(int cameraX, int cameraY)
{
	if (obj_state != IS_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}
}

OBJECT_TYPE Axe::getObjectType()
{
	return OBJECT_ITEM;
}


void Axe::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch(obj_state)
	{
	case IS_IDLE:
		break;

	case IS_ACTIVING:
		{
			width = p_sprite->getTexture()->getWidth();
			height = p_sprite->getTexture()->getHeight();
			pos_x_old = pos_x;
			pos_y_old = pos_y;

			pos_x += veloc_x * gameTime;
			veloc_y -= accel_y * gameTime;
			pos_y += veloc_y * gameTime + 1/2.0 * accel_y * gameTime * gameTime;

			Object* obj;
			list<Object*>::iterator i_obj;
			for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
			{
				obj = *i_obj;
					Box box(this->pos_x_old,
						this->pos_y_old - this->height, 
						width, 
						height, 
						veloc_x * gameTime, 
						veloc_y * gameTime + 1/2.0 * accel_y * gameTime * gameTime);

					Box block(obj->getPosX(),
						obj->getPosY() - obj->getHeight(),
						obj->getWidth(),
						obj->getHeight()
						);

					Box boxphase = Collision::GetSweptBroadphaseBox(box);
					if (Collision::AABBCheck(boxphase, block))
					{
						float normalx = 0;
						float normaly = 0;
						float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
						if (0 < collision_time && collision_time < 1)
						{
							if (obj->getObjectType() == OBJECT_LAND)
							{
								pos_y = pos_y_old + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;
								accel_y = 0;
								veloc_y = 0;
								veloc_x = 0;
								changeState(IS_REMOVE);
							}
							
							switch (obj->getIDObjType())
							{
							case 11:	//ghost
								{
									if (obj->getObjState() == ES_ACTIVING)
									{
										obj->changeState(ES_DIED);
									}
								}
								break;

							case 13:	//bat boss
							case 18:	//head boss
								{
									if (obj->getObjState() == ES_ACTIVING)
									{
										//obj->changeState(ES_DIED);
										int blood = obj->getBlood();
										blood--;
										obj->setBlood(blood);
										if (obj->getBlood() == 0)
										{
											obj->changeState(ES_DIED);
										}
										else
										{
											obj->changeState(ES_PAUSE);
										}
									}
								}
								break;


							}

						}
				}//end if
			}//end for

			pos_x_old = pos_x;
			pos_y_old = pos_y;
			
		}
		break;

	}
	p_sprite->update(gameTime);

}

//void Axe::updatePosition(list<Object*>* obj_list, DxInput* input, float gameTime, int pos_x, int pos_y)
//{
//	switch(obj_state)
//	{
//	case IS_IDLE:
//		
//		break;
//
//	case IS_ACTIVING:
//		this->pos_x = pos_x;
//		this->pos_y = pos_y;
//
//		p_sprite->update(gameTime);
//		countFrameRod = p_sprite->getIndexFrame();
//		if (countFrameRod == 2)
//		{
//			attack(obj_list, gameTime);
//		}
//		else if (countFrameRod >= 3)
//		{
//			p_sprite->setIndexFrame(0);
//
//			countFrameRod = 0;
//
//			changeState(IS_REMOVE);
//		}
//
//		break;
//
//
//	}
//
//	//p_sprite->update(gameTime);
//
//}
//

 

void Axe::changeState(int state)
{
	obj_state = state;
	switch(state)
	{
	case IS_ACTIVING:		
		veloc_x = AXE_VX;
		accel_y = AXE_GRAVITY;
		veloc_y = 0;
		break;	 

	case IS_REMOVE:
		break;
	}
}

//void Axe::attack(list<Object*>* obj_list, float time)
//{
//	Object *obj;
//	list<Object*>::iterator i_obj;
//
//	//Va cham Thong Thuong
//	for (i_obj = obj_list->begin(); i_obj != obj_list->end(); i_obj++)
//	{
//		obj = *i_obj;
//
//		if (obj->getObjectType() == OBJECT_ENEMY || obj->getObjectType() == OBJECT_ITEM)		
//		{ 
//			
//			Box box(obj->getPosX(),
//				obj->getPosY() - obj->getHeight(),
//				obj->getWidth(),
//				obj->getHeight(),
//				obj->getVelocX() * time,
//				obj->getVelocY() * time + 1/2.0 * obj->getAccelY() * time * time
//				);
//
//			Box block(this->pos_x,
//				this->pos_y - this->height, 
//				this->width, 
//				this->height
//				);
//
//			Box boxphase = Collision::GetSweptBroadphaseBox(box);			 
//			//if (Collision::AABBCheck(boxphase, block)){}
//
//			if (Collision::AABBCheck(boxphase, block))//if (Collision::RectVsRect(box, block))
//			{
//				switch (obj->getIDObjType())
//				{
//				case 11:	//ghost
//					{
//						if (obj->getObjState() == ES_ACTIVING)
//						{
//							obj->changeState(ES_DIED);
//						}
//					}
//					break;
//
//				case 12:	//bat boss
//					{
//						if (obj->getObjState() == ES_ACTIVING)
//						{
//							obj->changeState(ES_DIED);
//						}
//					}
//					break;
//
//				case 21: //Heart
//					{
//						if (obj->getObjState() == IS_IDLE)
//						{
//							obj->changeState(IS_ACTIVING); 
//						}
//
//					}
//					break;
//
//				}
//
//			}//end if
//
//		}//end if
//
//	}//end for
//}